package com.example.anni.zhuzhoujia.xiaowan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Anni on 2016/6/28.
 */
public class FansNumRecyclerView extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<Map<String, String>> mDatas;
    private FansNumRecyclerViewAdapter mFansNumRecyclerViewAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fans_recyclerview_activity);
        initData();
        initView();
        initToolbar();
        //设置RecyclerView的布局管理
        mRecyclerView.setLayoutManager(
                new StaggeredGridLayoutManager(6, StaggeredGridLayoutManager.HORIZONTAL));
        //设置下划线
        /*mRecyclerView.addItemDecoration(
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));*/
        //设置动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    private void initToolbar() {
        TextView tvLeftTitle;
        tvLeftTitle = (TextView) findViewById(R.id.tv_left_title);
        tvLeftTitle.setText("粉丝人数");
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_fans);
    }

    private void initData() {
        mDatas = new ArrayList<>();
        RequestParams params =
                new RequestParams("http://tr.zzapi.gson.cn/api/user/get/fans");
        String uid = getIntent().getStringExtra("uid");
        params.addQueryStringParameter("page", "1");
        params.addQueryStringParameter("rows", "15");
        params.addQueryStringParameter("uid", uid);
        x.http().get(params, new CallBackAdapter<String>() {
            @Override
            public void onSuccess(String s) {
                //Log.e("s-----------",s);
                JSONObject obj = JSON.parseObject(s);
                if (obj.getBoolean("success")) {
                    JSONArray arr = obj.getJSONArray("data");
                    for (int i = 0; i < arr.size(); i++) {
                        Map<String, String> map = new HashMap<>();
                        map.put("avatar", arr.getJSONObject(i).getString("avatar"));
                        map.put("gender", arr.getJSONObject(i).getString("gender"));
                        map.put("id", arr.getJSONObject(i).getString("id"));
                        map.put("nickName", arr.getJSONObject(i).getString("nickName"));
                        map.put("area", arr.getJSONObject(i).getString("area"));
                        map.put("dateline", arr.getJSONObject(i).getString("dateline"));
                        mDatas.add(map);

                    }
                    //设置RecyclerView的设配器
                    mFansNumRecyclerViewAdapter = new FansNumRecyclerViewAdapter(FansNumRecyclerView.this, mDatas);
                    mRecyclerView.setAdapter(mFansNumRecyclerViewAdapter);

                    mFansNumRecyclerViewAdapter.setOnItemClickListener(new FansNumRecyclerViewAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            mFansNumRecyclerViewAdapter.showPersonalInformation(position);
                        }

                        @Override
                        public void onItemLongClick(View view, int position) {


                        }
                    });
                }
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                throwable.printStackTrace();
            }
        });
    }

}
